<?php
include "game.php";
class gameWebController extends gameController
{
    public function __construct()
    {
        parent::__construct('show');
        $this->printResult = TRUE;
        $this->in_user();
    }
    public function doARshare(){
        $this->printResult = false;
        $this->load('game')->checkWxPubOpenId();
        
        $to_uid = common::request('uid','G','');
        $monster_id = common::request('monster_id','G','');
        $playInfo = json_decode($_COOKIE['wxpo'],true);
        
        $tipsString = [
            '我觉得最近我训练完以后腿好疼，想买本《精准拉伸：疼痛消除和损伤预防的针对性练习》来学习一下，可以买给我吗？',
            '最近我觉得我胖了，我想看一下《塑造你的完美身材：女性HIIT权威指南》，TA们都推荐我看一下~',
            '我想试试跑个马拉松，但是不知道怎么做，想参考下《梅布的马拉松入门教程：像波马冠军一样跑步、思考和饮食》可以吗？',
            '我想要成为像傅园慧一样的拥有洪荒之力的战斗“鸡”，你能给我买一本她推荐的教程《游泳运动从入门到精通》吗？',
            '据说练瑜伽的人类人手一本《瑜伽解剖学》，你能帮我借来看看吗？'
        ];
        $tostString = [
            '太棒了！按照里面奥运冠军的方法拉伸以后，我的腿腿果然不疼了呢~我还要去跑个10公里~唧~',
            '好棒！里面的训练指导循序渐进，我觉得我明显变得更匀称了！谢谢你~',
            '谢谢谢谢，我已经明白我要怎么做了~',
            '唧唧~我已经获得了洪荒之力~',
            '我已经知道了所有瑜伽姿势的奥秘，谢谢你~'
        ];
        $stringKey = array_rand($tipsString);
        //剩余次数
        $today_begin = strtotime('today');
        $times = $this->db->getOne("select count(id) from r_game_care_record_wx where from_openid='{$playInfo['openid']}' and to_uid='{$to_uid}' and addtime>='$today_begin' limit 1");
        
        $this->view->assign('address',$address);
        $this->view->assign('left_times',$times < 3 ? 3-$times : 0);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->assign('tipsString',$tipsString[$stringKey]);
        $this->view->assign('tostString',$tostString[$stringKey]);
        $this->view->assign('to_uid',$to_uid);
        $this->view->assign('monster_id',$monster_id);
        $this->view->display('game/ar-share');
    }
    
    /*
     * 照料
     */
    public function docareMonster(){
        $this->load('game')->checkWxPubOpenId();
        $playInfo = json_decode($_COOKIE['wxpo'],true);
        $monster_id = common::request('monster_id','P',0);
        if(!$monster_id){
            $_ENV['error']->getErrors(101);
            exit;
        }
            
        $game_mod = $this->load('game');
        $monsterInfo = $game_mod->getMonsterInfo($monster_id);
        if(!$monsterInfo){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $to_uid = $monsterInfo['uid'];
        
        $today_begin = strtotime('today');
        $times = $this->db->getOne("select count(id) from r_game_care_record_wx where from_openid='{$playInfo['openid']}' and to_uid='{$to_uid}' and addtime>='$today_begin' limit 1");
        $res = $times < 3 ? $game_mod->careMonsterWx($playInfo['openid'],$playInfo['nickname'],$playInfo['header'],$monster_id) : 0;
        $times++;
        $this->result['result']['giftInfo'] = $game_mod->getGift($this->uid,null,1);
        $this->result['result']['left_times'] = $times < 3 ? 3-$times : 0;
        $this->result['result']['success'] = $res ? 1 : 0;
    }
    
    public function doARgiveBuff(){
        $this->printResult = false;
        $this->load('game')->checkWxPubOpenId();
        $playInfo = json_decode($_COOKIE['wxpo'],true);
        
        $address = common::request('address','G','');
        $to_uid = common::request('uid','G','');
        $monster_id = common::request('monster_id','G','');
        $poi = common::request('poi','G','');
        $boss_type = common::request('boss_type','G','');
        $uid = $this->uid;
         $tipsString = [
            '嗷嗷嗷~人类，你们想要击败我还早了100万年！你们这是自寻死路！',
            '嗷嗷嗷嗷~今年应该是我们年兽做主了！我讨厌你们这群有莫名其妙正义感的人类！',
            '嗷嗷嗷嗷嗷嗷~随便一声吼，人类抖三抖~随便街上走一走，带走一波单身狗……'
        ];
        $stringKey = array_rand($tipsString);
        
        
        $this->view->assign('address',$address);
        $this->view->assign('poi',$poi);
        $this->view->assign('boss_type',$boss_type);
        $this->view->assign('tipsString',$tipsString[$stringKey]);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->assign('to_uid',$to_uid);
        $this->view->assign('monster_id',$monster_id);
        $this->view->assign('uid',$uid);
        $this->view->display('game/ar-giveBuff');
    }
    
    /*
     * 助力
     */
    public function dogiveBuff(){
        $to_uid = common::request('to_uid','P','',true);
        $this->load('game')->checkWxPubOpenId();
        $playInfo = json_decode($_COOKIE['wxpo'],true);
        
        if(!$to_uid || !is_numeric($to_uid)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $today_begin = strtotime('today');
        $exists = $this->db->getOne("select id from r_game_buff_record_wx where from_openid='{$playInfo['openid']}' and to_uid='{$to_uid}' and addtime>='$today_begin' limit 1");
        
        if(!$exists){
            $this->load('game')->giveBuff($playInfo['openid'],$playInfo['nickname'],$playInfo['header'],$to_uid);
        }
        $this->result['result'] = ['success'=>1];
        $this->result['result']['giftInfo'] = $this->load('game')->getGift($playInfo['openid'],null,1);
    }
    
    public function doARrank(){
        $this->printResult = false;
        
        $to_uid = common::request('uid','G','');
        $monster_id = common::request('monster_id','G','');
        $uid = $this->uid;
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->assign('to_uid',$to_uid);
        $this->view->assign('monster_id',$monster_id);
        $this->view->assign('uid',$uid);
        $this->view->display('game/ar-rank');
    }
    public function doARcard(){
        $this->printResult = false;
        $this->load('game')->checkWxPubOpenId();
        
        $to_uid = common::request('uid','G','');
        $monster_id = common::request('monster_id','G','');
        $playInfo = json_decode($_COOKIE['wxpo'],true);
        
        $tips = [
            '新的1年开始，祝好事接2连3，心情4季如春，生活5颜6色，7彩缤纷，偶尔8点小财，烦恼抛到9霄云外!请接受我10心10意的祝福。',
            '送你一件外套，前面是平安，后面是幸福，吉祥是领子，如意是袖子，快乐是扣子，口袋里满是温暖，穿上吧，让它相伴你的每一天!',
            '天增岁月人增寿，春满乾坤福满门。三羊开泰送吉祥，五福临门财源茂。恭祝新春快乐，幸福安康！！',
            '天增岁月人增寿，春满乾坤福满门。三羊开泰送吉祥，五福临门财源茂。恭祝新春快乐，幸福安康！！',
            '祝你一帆风顺，二龙腾飞，三羊开泰，四季平安，五福临门，六六大顺，七星高照，八方来财，九九同心，十全十美，百事亨通，千事吉祥，万事如意!',
            '酒越久越醇，水越流越清，世间沧桑越流越淡，朋友情谊越久越真。衷心祝福吾友度过一个祥和美满的新春!',
            '对你的思念像袅袅的轻烟不绝如缕，对你的祝福是潺潺的小溪叮咚作响。或许岁月将往事褪色，或许空间将彼此隔离。但值得珍惜的依然是你给我的情谊。再次对你说声：新春快乐!',
            '值此新春之际，祝您：大财、小财、意外财，财源滚滚;亲情、友情、爱情，情情如意;官运、福运、桃花运，天天走运;爱人、亲人、友人，人人平安!',
            '拜年啦!咱都老相识了，客套话就不说了，给你送句实实在在的祝福：兜里有钱，办事有人，生活有乐，事业有成，朋友有情，人生有福!',
            '我的祝福是个宝，幸运人儿能收到，鸡年事业直登高，金银财宝装满包，家人平安开心笑，身体健康金钟罩，天天好运来围绕!',
            '愿你有高跟鞋也有跑鞋，喝茶也喝酒。愿你有勇敢的朋友，有牛逼的对手。愿你对过往的一切情深意重，但从不回头。愿你特别美丽，特别平静，特别凶狠，也特别温柔。'
        ];
        
        $client_mod = $this->load('client');
        $this->view->assign('nickname',$playInfo['nickname']);
        $this->view->assign('toUserInfo',$client_mod->getUserInfo($to_uid));
        $this->view->assign('tipsString',$tips[array_rand($tips)]);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->assign('to_uid',$to_uid);
        $this->view->assign('monster_id',$monster_id);
        $this->view->assign('uid',$uid);
        $this->view->display('game/ar-card');
    }
    public function doARqa(){
        $uid = $this->uid;
        $this->printResult = false;
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->assign('uid',$uid);
        $this->view->display('game/ar-qa');
    }
    public function doARaddress(){
        $uid = $this->uid;
        $rid = common::request('rid','R');   //中奖流水id
        $adressInfo = array('giftName' => '', 'giftImag' => '', 'receiver' => '', 'phone' => '', 'province' => '', 'city' => '', 'district' => '', 'address' => '');
        if($rid){
            $sql = "select r.open_id,g.name,g.img from r_game_gift_record r left join r_game_gifts g on r.gift_id = g.gid where r.id = {$rid} limit 1";
            $giftInfo = $this->db->getRow($sql);
            $adressInfo['giftName'] = $giftInfo['name'];
            $adressInfo['giftImag'] = $giftInfo['img'];
            $sql2 = "select * form r_game_gift_record where open_id = {$giftInfo['open_id']} and phone != '' order by addtime desc limit 1";
            $recordInfo = $this->db->getRow($sql2);
            if($recordInfo){
                $adressInfo = array(
                    'receiver' => $recordInfo['receiver'],
                    'phone' => $recordInfo['phone'],
                    'province' => $recordInfo['province'],
                    'city' => $recordInfo['city'],
                    'district' => $recordInfo['district'],
                    'address' => $recordInfo['address']
                );
            }
        }

        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->assign('uid',$uid);
        $this->view->assign('rid',$rid);
        $this->view->assign('adressInfo',$adressInfo);
        $this->view->display('game/ar-address');
    }
    
    /**
    * 微信回调
    */
    public function dowxCallback()
    {
        $this->loadLib('wx');
        $code = $_GET['code'];
        $state = $_GET['state'];
        $wx = new wx();
        $tokenInfo = $wx->getToken($code, $state);
        $token = $tokenInfo['access_token'];
        $openid = $tokenInfo['openid'];
        $userInfo = $wx->getUserInfo($token, $openid);
        $nickname = 'wx'.time();
        $header = 'http://irun.hupu.com/static/images/4.jpg';
        if(!empty($userInfo)){
            isset($userInfo['nickname']) && $nickname = $userInfo['nickname'];
            isset($userInfo['headimgurl']) && $header = $userInfo['headimgurl'];
        }
        $playInfo = array(
            'openid' => $openid,
            'nickname' => $nickname,
            'header' => $header
        );
        $playInfo = json_encode($playInfo);
        setcookie('wxpo', $playInfo, time() + 86400, '/', '.hupu.com', FALSE, TRUE);
        setcookie('wxpo', $playInfo, time() + 86400, '/', '.ijoggers.com', FALSE, TRUE);
        
        $rurl = isset($_COOKIE['rurl']) ? $_COOKIE['rurl'] : '';
        common::go($rurl);
    }
}